/* SPDX-License-Identifier: GPL-2.0 */
/* Copyright(c) 2021 Huawei Technologies Co., Ltd */

#ifndef OSSL_KNL_H
#define OSSL_KNL_H

#ifdef __LINUX__
#include "ossl_knl_linux.h"
#endif
#ifdef _LLT_TEST_
#include "llt_lib_kernel_stub.h"
#endif
#include <linux/types.h>
#ifdef _WIN32 /* Defined for applications for Win32 and Win64. Always defined. */
#define WIN
#endif

#if defined(WIN) || defined(VMWARE)
#define WIN_OR_VMWARE
#endif

#if defined(WIN) || defined(VMWARE) || defined(__UEFI__)
#define WIN_OR_VMWARE_OR_UEFI
#endif

#if (defined(WIN) || defined(VMWARE)) && !defined(__HIFC__)
#define WIN_OR_VMWARE_AND_NONHIFC
#endif

#if defined(WIN) || defined(__UEFI__)
#define WIN_OR_UEFI
#endif

#ifdef WIN
#include "ossl_knl_win.h"
#ifdef __HIFC__
#include "ossl_knl_fc_win.h"
#else
#include "ossl_knl_nic_win.h"
#endif
#endif

#ifdef VMWARE
#ifndef __HIFC__
#include "ossl_knl_vmware.h"
#include "hinic_interrupt.h"
#include "hinic_pci_map.h"
#else
#include "ossl_knl_vmware.h"
#include "hifc3_comm.h"
#endif
#endif

#ifdef __UEFI__
#include "ossl_knl_uefi.h"
#endif

#ifndef VMWARE
#define sdk_err(dev, format, ...) dev_err(dev, "[COMM]" format, ##__VA_ARGS__)
#define sdk_warn(dev, format, ...) dev_warn(dev, "[COMM]" format, ##__VA_ARGS__)
#define sdk_notice(dev, format, ...) dev_notice(dev, "[COMM]" format, ##__VA_ARGS__)
#define sdk_info(dev, format, ...) dev_info(dev, "[COMM]" format, ##__VA_ARGS__)

#define nic_err(dev, format, ...) dev_err(dev, "[NIC]" format, ##__VA_ARGS__)
#define nic_warn(dev, format, ...) dev_warn(dev, "[NIC]" format, ##__VA_ARGS__)
#define nic_notice(dev, format, ...) dev_notice(dev, "[NIC]" format, ##__VA_ARGS__)
#define nic_info(dev, format, ...) dev_info(dev, "[NIC]" format, ##__VA_ARGS__)
#endif

#ifndef __LINUX__
struct msi_msg {
	u32	address_lo;
	u32	address_hi;
	u32	data;
};

/* MSI-X Table entry format */
#define  PCI_MSIX_ENTRY_SIZE		16
#define  PCI_MSIX_ENTRY_LOWER_ADDR	0
#define  PCI_MSIX_ENTRY_UPPER_ADDR	4
#define  PCI_MSIX_ENTRY_DATA		8

#endif

#if !defined(__UEFI__) && !defined(__LINUX__) && !defined(VMWARE)
#define DIV_ROUND_UP(n, d) (((n) + (d)-1) / (d))
#define BITS_PER_BYTE 8
#define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
#define DECLARE_BITMAP(name, bits) unsigned long name[BITS_TO_LONGS(bits)]

#define small_const_nbits(nbits) ((nbits) <= (sizeof(unsigned long) * BITS_PER_BYTE))

static inline void bitmap_copy(unsigned long *dst, const unsigned long *src, int nbits)
{
    if (small_const_nbits(nbits))
        *dst = *src;
    else {
        int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
        (void)memcpy_s(dst, (unsigned long)len, src, (unsigned long)len);
    }
}

static inline void bitmap_zero(unsigned long *dst, int nbits)
{
    if (small_const_nbits(nbits))
        *dst = 0UL;
    else {
        int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
        (void)memset_s(dst, (unsigned long)len, 0, (unsigned long)len);
    }
}
#endif

#ifndef BIG_ENDIAN
#define BIG_ENDIAN    0x4321
#endif

#ifndef LITTLE_ENDIAN
#define LITTLE_ENDIAN    0x1234
#endif

#ifdef BYTE_ORDER
#undef BYTE_ORDER
#endif
/* X86 */
#define BYTE_ORDER    LITTLE_ENDIAN
#define USEC_PER_MSEC	1000L
#define MSEC_PER_SEC	1000L

/* Waiting for 50 us */
#define WAIT_USEC_50    50L

#endif /* OSSL_KNL_H */
